package com.cw.jeeyt.controller.process;

import org.flowable.idm.engine.impl.persistence.entity.UserEntityImpl;
import org.flowable.ui.common.model.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cw.jeeyt.api.dto.response.JsonResultDto;
import com.cw.jeeyt.common.StringUtils;
import com.cw.jeeyt.common.web.BaseController;
import com.cw.jeeyt.service.bo.process.ActDeModelBo;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.intf.process.ActDeModelService;

import static com.cw.jeeyt.common.web.Servlets.renderResult;

/**
 * 流程模型相关Controller
 *
 * @author liuruijun
 * @version 2018-07-29
 */
@Controller
@RequestMapping(value = "/act/model")
public class DeModelController extends BaseController {

    @Autowired
    private ActDeModelService deModelService;

    @ModelAttribute
    public ActDeModelBo get(@RequestParam(required = false) String id) throws Exception {
        ActDeModelBo entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = deModelService.getActDeModelById(id);
        }
        if (entity == null) {
            entity = new ActDeModelBo();
        }
        return entity;
    }

    @RequestMapping(value = "account", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public UserRepresentation account() {
        UserEntityImpl fUser = new UserEntityImpl();
        fUser.setId("admin");
        fUser.setFirstName("Test");
        fUser.setLastName("Administrator");
        fUser.setEmail("admin@flowable.org");
        UserRepresentation userRepresentation = new UserRepresentation(fUser);
        return userRepresentation;
    }

    /**
     * 根据Model部署流程
     */
    @RequestMapping(value = "deploy")
    @ResponseBody
    public JsonResultDto deploy(ActDeModelBo deModel, String category) {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        String result = deModelService.doDeploy(deModel.getId(), category);
        jsonResultDto.setMsg(result);
        jsonResultDto.setSuccess(true);
        return jsonResultDto;
    }

    /**
     * 删除Model
     *
     * @param deModel
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public JsonResultDto delete(ActDeModelBo deModel) throws Exception {
        JsonResultDto jsonResultDto = new JsonResultDto(false);
        deModelService.delete(deModel);
        jsonResultDto.setMsg("删除模型成功");
        jsonResultDto.setSuccess(true);
        return jsonResultDto;
    }

    @RequestMapping(value = "choose/category")
    public String chooseCategory(String modelId, Model model) {
        model.addAttribute("modelId", modelId);
        model.addAttribute("PageDict", new PageDictDirective());
        return "process/processChooseCategory";
    }
}
